# -*- coding: utf-8 -*- 
# 作者:飞虎
# 开发时间:2022/4/28 22:47

from PyQt5.Qt import *


class Window(QWidget):
	def __init__(self):
		super().__init__()
		self.setWindowTitle('QObject学习')
		self.resize(500, 500)
		self.setup_ui()

	def setup_ui(self):
		# self.QObject继承结构测试()
		# self.QObject对象名称和属性的操作()
		# self.QObject父子关系()
		# self.qObject信号操作()
		self.QObject类型判定()
		# self.QObject对象删除()

	def QObject继承结构测试(self):
		# QObject.__subclasses__()

		mros = QObject.mro()
		for mro in mros:
			print(mro)

	def QObject对象名称和属性的操作(self):
		# 1. # 测试API
		# obj = QObject()
		# obj.setObjectName('notice')
		# print(obj.objectName())
		#
		# obj.setProperty('notice_level','error')
		# obj.setProperty('notice_level2','warning')
		#
		# print(obj.property('notice_level'))
		#
		# print(obj.dynamicPropertyNames())
		# # 测试API

		# 2.案例演示
		with open("QObject.qss", "r") as f:
			qApp.setStyleSheet(f.read())
		label = QLabel(self)
		label.setObjectName('notice')
		label.setProperty('notice_level', 'warning')
		label.setText("社会我飞虎")

		label2 = QLabel(self)
		label2.setObjectName('notice')
		label2.setProperty('notice_level', 'error')

		label2.move(100, 100)
		label2.setText("人狠话不多")

		label3 = QLabel(self)
		label3.move(150, 150)
		label3.setText('XXXXX')

		btn = QPushButton(self)
		btn.setText('按钮')
		btn.move(50, 50)

	# label.setStyleSheet("font-size:20px;color:red;")

	def QObject父子关系(self):
		# 测试API
		# obj0 = QObject()
		# obj1 = QObject()
		# obj2 = QObject()
		# obj3 = QObject()
		# obj4 = QObject()
		# obj5 = QObject()
		# print('obj0',obj0)
		# print('obj1',obj1)
		# print('obj2',obj2)
		# print('obj3',obj3)
		# print('obj4',obj4)
		# print('obj5',obj5)
		# obj1.setParent(obj0)
		# obj2.setParent(obj0)
		# obj2.setObjectName('2')
		#
		# obj3.setParent(obj1)
		# obj3.setObjectName('3')
		#
		# obj4.setParent(obj2)
		# obj5.setParent(obj2)
		#
		# # print(obj4.parent())
		# # print(obj0.children())
		# print(obj0.findChildren(QObject))
		# 测试API

		# 内存管理机制
		obj1 = QObject()
		self.obj1 = obj1
		obj2 = QObject()

		obj2.setParent(obj1)

		# 监听obj2对象被释放
		obj2.destroyed.connect(lambda: print('obj2对象被释放了'))

		del self.obj1

	def qObject信号操作(self):
		pass


		# self.obj = QObject()
		#
		# # obj.destroyed
		# # obj.objectNameChanged
		# # def destroy_cao(obj):
		# # 	print('对象被释放了',obj)
		# #
		# # self.obj.destroyed.connect(destroy_cao)
		# #
		# # del self.obj
		# def obj_name_cao(name):
		# 	print('对象名称发生的改变!',name)
		#
		# def obj_name_cao2(name):
		# 	print('对象名称发生的改变2!',name)
		#
		# self.obj.objectNameChanged.connect(obj_name_cao)
		# self.obj.objectNameChanged.connect(obj_name_cao2)
		#
		# print(self.obj.receivers(self.obj.objectNameChanged))
		#
		# # self.obj.setObjectName('xxx')
		#
		# # # self.obj.objectNameChanged.disconnect()
		# # print(self.obj.signalsBlocked(),"1")
		# # self.obj.blockSignals(True)
		# # print(self.obj.signalsBlocked(),"2")
		# #
		# # self.obj.setObjectName('ooo')
		# #
		# # # self.obj.objectNameChanged.connect(obj_name_cao)
		# # self.obj.blockSignals(False)
		# # print(self.obj.signalsBlocked(),"3")
		#
		#
		# self.obj.setObjectName('xxoo')

		# 信号与槽的案例
		# btn = QPushButton(self)
		# btn.setText('点击我')
		#
		# def cao():
		# 	print('点我干撒!')
		#
		# btn.clicked.connect(cao)

	def QObject类型判定(self):
		# API
		# obj = QObject()
		# w = QWidget()
		# btn = QPushButton()
		# label = QLabel()
		# objs = [obj,w,btn,label]
		# for o in objs:
		# 	# print(o.isWidgetType())
		# 	# print(o.inherits("QWidget"))
		# 	print(o.inherits("QPushButton"))
		# API

		# 案例
		label1= QLabel(self)
		label1.setText('社会我飞虎')
		label2= QLabel(self)
		label2.setText('人狠话不多')
		label2.move(50, 50)

		# del label2
		label2.deleteLater()

		btn = QPushButton(self)
		btn.setText('点我')
		btn.move(100,100)

		# for widget in self.findChildren(QLabel):
		for widget in self.children():
			# if widget.isWidgetType():
			if widget.inherits("QLabel"):
				widget.setStyleSheet('background-color:cyan;')


	def QObject对象删除(self):
		obj1 = QObject()
		self.obj1 = obj1
		obj2 = QObject()
		obj3 = QObject()

		obj3.setParent(obj2)
		obj2.setParent(obj1)

		obj1.destroyed.connect(lambda :print('obj1被释放了'))
		obj2.destroyed.connect(lambda :print('obj2被释放了'))
		obj3.destroyed.connect(lambda :print('obj3被释放了'))

		obj2.deleteLater()
		print(obj1.children())

if __name__ == '__main__':
	import sys

	app = QApplication(sys.argv)
	window = Window()
	# window = QWidget()

	# 连接窗口变化的信号与槽
	# def cao(title):
	# 	# print('窗口标题变化了',title)
	# 	window.blockSignals(True)
	# 	window.setWindowTitle('feihu-'+title)
	# 	window.blockSignals(False)
	#
	#
	# window.windowTitleChanged.connect(cao)
	#
	# window.setWindowTitle('Hello Sz')
	# window.setWindowTitle('Hello Sz2')
	# window.setWindowTitle('Hello Sz3')

	window.show()
	# win1 = QWidget()
	# win1.setStyleSheet('background-color:red;')
	# win1.setWindowTitle('红色')
	# win1.show()
	#
	# win2 = QWidget()
	# win2.setStyleSheet('background-color:green;')
	# # win2.setParent(win1)
	# win2.setWindowTitle('绿色')
	#
	# win2.resize(200, 200)
	# win2.show()

	# win_root = QWidget()
	#
	# label1= QLabel(win_root)
	# label1.setText('label1')
	#
	# label2= QLabel(win_root)
	# label2.setText('label2')
	# label2.move(50,50)
	#
	# label3= QLabel(win_root)
	# label3.setText('label3')
	# label3.move(80,80)
	#
	#
	#
	# btn = QPushButton(win_root)
	# btn.setText('btn')
	# btn.move(100,100)
	#
	# for sub_widget in win_root.findChildren(QLabel):
	# 	print(sub_widget)
	# 	sub_widget.setStyleSheet('background-color:cyan')
	#
	# win_root.show()

	sys.exit(app.exec_())
